﻿using System.Windows.Forms;
using Outlook.WorkItems.Extension.Manager;
using Outlook.WorkItems.Extension.Managers;
using System.Collections.Generic;
using System.Linq;

namespace Outlook.WorkItems.Extension.Controls
{
    public partial class TeamExplorerControl : TreeView
    {
        public TeamExplorerControl()
        {
            InitializeComponent();
            this.ImageList = imageList1;
            this.ImageIndex = 0;
            this.DoubleBuffered = true;
            this.ShowLines = false;
        }


        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        public void AddTeamServerNode(FavoriteTeamProject favorite, bool showReport, bool showDocumentLibrairies, bool showQuery, bool showFile)
        {
            TeamServerNode serverNode = new TeamServerNode(favorite.TeamServerName);
            this.Nodes.Add(serverNode);
            serverNode.Nodes.Add(new TeamProjectNode(favorite, showReport, showDocumentLibrairies, showQuery, showFile));
        }

        public void InitTreeView()
        {
            try
            {
                this.Nodes.Clear();
                List<FavoriteTeamProject> favorites = FavoritesTeamProjectManager.GetFavoritesProjects();

                var favoritesGrouped = favorites.GroupBy(f => f.TeamServerName);
                foreach (var gp in favoritesGrouped)
                {
                    TeamServerNode serverNode = new TeamServerNode(gp.Key);
                    this.Nodes.Add(serverNode);
                    foreach (var favorite in gp)
                    {
                        serverNode.Nodes.Add(new TeamProjectNode(favorite, false, false, true, false));
                    }
                }
            }
            catch (System.Exception ex)
            {
                using (ErrorForm frm = new ErrorForm(ex))
                {
                    frm.ShowDialog();
                }
            }
        }
    }
}
